/// 32bit 循环左移.
pub fn rotate_left(value: u32, shift: u32) -> u32 {
    (value << shift) | (value >> (32 - shift))
}

/// 31bit 循环左移.
pub fn rotate_left_31(value: u32, shift: u32) -> u32 {
    ((value << shift) | (value >> (31 - shift))) & 0x7fff_ffff
}

/// 31bit 模 2^31 - 1 的加法.
pub fn add_mod_2_31_dec1(a: u32, b: u32) -> u32 {
    let c = a + b;
    (c & 0x7fff_ffff) + (c >> 31)
}

/// 31bit 模 2^31 - 1 的左移.
pub fn left_mod_2_31_dec1(value: u32, shift: u32) -> u32 {
    let c = rotate_left_31(value, shift);
    (c & 0x7fff_ffff) + (c >> 31)
}

mod tests {
    #[test]
    fn test_rotate_left_31() {
        use super::rotate_left_31;
        assert_eq!(rotate_left_31(0x7fff_ffff, 1), 0x7fff_ffff);
    }
}
